home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / mmkeys / osx.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  4KB  |  104 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. import threading
  5. from gi.repository import GLib
  6. from _base import MMKeysBackend, MMKeysAction, MMKeysImportError
  7.  
  8. try:
  9.     from AppKit import NSKeyUp, NSSystemDefined, NSEvent
  10.     import Quartz
  11. except ImportError:
  12.     raise MMKeysImportError
  13.  
  14.  
  15. class OSXBackend(MMKeysBackend):
  16.     
  17.     def __init__(self, name, callback):
  18.         self._OSXBackend__eventsapp = MacKeyEventsTap(callback)
  19.         self._OSXBackend__eventsapp.start()
  20.  
  21.     
  22.     def cancel(self):
  23.         if self._OSXBackend__eventsapp is not None:
  24.             self._OSXBackend__eventsapp.stop()
  25.             self._OSXBackend__eventsapp = None
  26.  
  27.  
  28.  
  29. class MacKeyEventsTap(threading.Thread):
  30.     _EVENTS = {
  31.         16: MMKeysAction.PLAYPAUSE,
  32.         19: MMKeysAction.NEXT,
  33.         20: MMKeysAction.PREV }
  34.     
  35.     def __init__(self, callback):
  36.         super(MacKeyEventsTap, self).__init__()
  37.         self._callback = callback
  38.         self._tap = None
  39.         self._runLoopSource = None
  40.         self._event = threading.Event()
  41.  
  42.     
  43.     def _push_callback(self, action):
  44.         
  45.         def idle_call(action):
  46.             if self._tap:
  47.                 self._callback(action)
  48.             return False
  49.  
  50.         GLib.idle_add(idle_call, action)
  51.  
  52.     
  53.     def _event_tap(self, proxy, type_, event, refcon):
  54.         if type_ == Quartz.kCGEventTapDisabledByUserInput or type_ == Quartz.kCGEventTapDisabledByTimeout:
  55.             Quartz.CGEventTapEnable(self._tap, True)
  56.             return event
  57.         keyEvent = None.eventWithCGEvent_(event)
  58.         if keyEvent.subtype() is 8:
  59.             data = keyEvent.data1()
  60.             keyCode = (data & 0xFFFF0000L) >> 16
  61.             keyState = (data & 65280) >> 8
  62.             if keyCode in self._EVENTS:
  63.                 if keyState == NSKeyUp:
  64.                     self._push_callback(self._EVENTS[keyCode])
  65.                 return None
  66.         return event
  67.  
  68.     
  69.     def _loop_start(self, observer, activiti, info):
  70.         self._event.set()
  71.  
  72.     
  73.     def run(self):
  74.         self._tap = Quartz.CGEventTapCreate(Quartz.kCGSessionEventTap, Quartz.kCGHeadInsertEventTap, Quartz.kCGEventTapOptionDefault, Quartz.CGEventMaskBit(NSSystemDefined), self._event_tap, None)
  75.         if self._tap is None:
  76.             self._event.set()
  77.             return None
  78.         self._loop = None.CFRunLoopGetCurrent()
  79.         self._observ = Quartz.CFRunLoopObserverCreate(None, Quartz.kCFRunLoopEntry, False, 0, self._loop_start, None)
  80.         Quartz.CFRunLoopAddObserver(self._loop, self._observ, Quartz.kCFRunLoopCommonModes)
  81.         self._runLoopSource = Quartz.CFMachPortCreateRunLoopSource(None, self._tap, 0)
  82.         Quartz.CFRunLoopAddSource(self._loop, self._runLoopSource, Quartz.kCFRunLoopDefaultMode)
  83.         Quartz.CGEventTapEnable(self._tap, True)
  84.         Quartz.CFRunLoopRun()
  85.  
  86.     
  87.     def stop(self):
  88.         '''Call once from the main thread to stop the thread.
  89.         After this returns no callback will be called anymore.
  90.         '''
  91.         self._event.wait()
  92.         if self._tap is None:
  93.             return None
  94.         None.CGEventTapEnable(self._tap, False)
  95.         self._tap = None
  96.         Quartz.CFRunLoopRemoveSource(self._loop, self._runLoopSource, Quartz.kCFRunLoopDefaultMode)
  97.         self._runLoopSource = None
  98.         Quartz.CFRunLoopRemoveObserver(self._loop, self._observ, Quartz.kCFRunLoopCommonModes)
  99.         self._observ = None
  100.         Quartz.CFRunLoopStop(self._loop)
  101.         self._loop = None
  102.  
  103.  
  104.